package com.frank.oj.service.entity.training.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.frank.model.entity.judge.Judge;
import com.frank.model.entity.training.TrainingCategory;
import com.frank.model.entity.training.TrainingProblem;
import com.frank.oj.mapper.TrainingProblemMapper;
import com.frank.oj.model.vo.ProblemFullScreenListVO;
import com.frank.oj.model.vo.ProblemVO;
import com.frank.oj.service.entity.judge.JudgeEntityService;
import com.frank.oj.service.entity.training.TrainingProblemEntityService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static com.frank.oj.manager.ContestFileManager.distinctByKey;

/**
 * @author frank
 * @Date 2024/3/23
 */
@Service
public class TrainingProblemEntityServiceImpl extends ServiceImpl<TrainingProblemMapper, TrainingProblem> implements TrainingProblemEntityService {
    @Resource
    private TrainingProblemMapper trainingProblemMapper;
    @Resource
    private JudgeEntityService judgeEntityService;

    @Override
    public List<TrainingProblem> getTrainingListAcceptedCountByUid(List<Long> tidList, String currentUid) {
        return trainingProblemMapper.getTrainingListAcceptedCountByUid(tidList, currentUid);
    }

    @Override
    public List<TrainingProblem> getPrivateTrainingProblemLisByPid(Long pid, String uid) {
        return trainingProblemMapper.getPrivateTrainingProblemListByPid(pid, uid);
    }

    @Override
    public List<Long> getTrainingProblemIdList(Long id) {
        return trainingProblemMapper.getTrainingProblemCount(id);
    }

    @Override
    public Integer getUserTrainingACProblemCount(String uid, Long gid, List<Long> pidList) {
        if (CollectionUtils.isEmpty(pidList)) {
            return 0;
        }

        QueryWrapper<Judge> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("distinct pid")
                .lambda().in(Judge::getPid, pidList)
                .eq(Judge::getCid, 0)
                .eq(Judge::getUid, uid)
                .eq(Judge::getStatus, 0);

        if (gid == null) {
            queryWrapper.isNull("gid");
        } else {
            queryWrapper.eq("gid", gid);
        }

        return judgeEntityService.count(queryWrapper);
    }

    @Override
    public List<ProblemVO> getTrainingProblemList(Long tid) {
        List<ProblemVO> trainingProblemList = trainingProblemMapper.getTrainingProblemList(tid);

        return trainingProblemList.stream().filter(distinctByKey(ProblemVO::getPid)).collect(Collectors.toList());
    }

    @Override
    public List<TrainingProblem> getGroupTrainingListAcceptedCountByUid(List<Long> tidList, Long gid, String uid) {
        return trainingProblemMapper.getGroupTrainingListAcceptedCountByUid(tidList, gid, uid);
    }

    @Override
    public List<ProblemFullScreenListVO> getTrainingFullScreenProblemList(Long tid) {
        return trainingProblemMapper.getTrainingFullScreenProblemList(tid);
    }

}
